package com.nageoffer.shortlink.project.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nageoffer.shortlink.project.dao.entity.LinkAccessLogsDO;
import com.nageoffer.shortlink.project.dao.entity.LinkAccessStatsDO;
import com.nageoffer.shortlink.project.dto.req.ShortLinkGroupStatsAccessRecordReqDTO;
import com.nageoffer.shortlink.project.dto.req.ShortLinkGroupStatsReqDTO;
import com.nageoffer.shortlink.project.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Mapper
public interface LinkAccessLogsMapper extends BaseMapper<LinkAccessLogsDO> {
    /**
     * 单链接高频ip访问记录
     *
     * @param requestParam
     * @return
     */
    List<HashMap<String, Object>> listTopIpByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 获取访客访问类型详情
     *
     * @param requestParam
     * @return
     */
    HashMap<String, Object> findUvTypeCntByShortLink(@Param("param") ShortLinkStatsReqDTO requestParam);

    /**
     * 根据分组获取指定日期内PV、UV、UIP数据
     *
     * @param requestParam
     * @return
     */
    LinkAccessStatsDO findPvUvUidStatsByGroup(@Param("param") ShortLinkGroupStatsReqDTO requestParam);

    /**
     * 根据分组获取指定日期内高频访问IP数据
     *
     * @param requestParam
     * @return
     */
    List<HashMap<String, Object>> listTopIpByGroup(@Param("param") ShortLinkGroupStatsReqDTO requestParam);

    /**
     * 根据分组获取指定日期内短链接历史记录
     */
    @Select("""
                SELECT 
                    tlal.* 
                FROM 
                    t_link tl 
                    INNER JOIN t_link_access_logs tlal ON tl.full_short_url = tlal.full_short_url 
                WHERE 
                    tl.gid = #{param.gid} 
                    AND tl.del_flag = '0' 
                    AND tl.enable_status = '0' 
                    -- 拼接当天开始时间（00:00:00）和结束时间（23:59:59）
                    AND tlal.create_time BETWEEN 
                        CONCAT(#{param.startDate}, ' 00:00:00') 
                        AND CONCAT(#{param.endDate}, ' 23:59:59') 
                ORDER BY 
                    tlal.create_time DESC
            """)
    IPage<LinkAccessLogsDO> selectGroupPage(@Param("param") ShortLinkGroupStatsAccessRecordReqDTO requestParam);

    /**
     * 获取分组用户信息是否新老访客
     */
    @Select("""
                <script>
                SELECT 
                    tlal.user,
                    CASE 
                        WHEN MIN(tlal.create_time) BETWEEN CONCAT(#{startDate}, ' 00:00:00') AND CONCAT(#{endDate}, ' 23:59:59') THEN '新访客' 
                        ELSE '老访客' 
                    END AS uvType 
                FROM 
                    t_link tl 
                INNER JOIN 
                    t_link_access_logs tlal ON tl.full_short_url = tlal.full_short_url 
                WHERE 
                    tl.gid = #{gid} 
                    AND tl.del_flag = '0' 
                    AND tl.enable_status = '0' 
                    AND tlal.user IN 
                    <foreach item='item' index='index' collection='userAccessLogsList' open='(' separator=',' close=')'> 
                        #{item} 
                    </foreach> 
                GROUP BY 
                    tlal.user;
                </script>
            """)
    List<Map<String, Object>> selectGroupUvTypeByUsers(
            @Param("gid") String gid,
            @Param("startDate") String startDate,
            @Param("endDate") String endDate,
            @Param("userAccessLogsList") List<String> userAccessLogsList
    );
}
